	$(function() {	 
		//全站标题SEO
		readSeoList();
		//全部修改记录
		readSeoListPage(currentPage);
	});

	var baseUrl = "";
	//当前显示第几页
	var currentPage=1;
	//一共多少页数据
	var totalPage;
	
	//全站标题SEO
	function readSeoList(){
		$.ajax({
			type: "POST",
			dataType: "JSON",
			url:baseUrl+'seoController/selectSeoList',
			contentType:'application/json;charset=UTF-8',
			success: function (result) {
				if(result!=null){
					var str = "";
					var show = "";
					var strs = new Array();
					for(var i=0;i<result.length;i++){
						str+="<div class=\"layui-colla-item\" data_id=\""+result[i].id+"\">";
						str+="<div class=\"layui-colla-title_1\" id=\"title"+result[i].id+"\">";
						str+="<h5>"+result[i].name+"</h5>";
						str+="<span class=\"datatime\">最近修改：<em>"+result[i].update+"</em></span>";
						str+="</div>";
						if(i==0){
							show = "layui-show";
						}else{
							show = "";
						}
						str+="<div class=\"layui-colla-content  "+show+"\" id=\"disp"+result[i].id+"\">";
						str+="<div class=\"control-group\">";
						str+="<label class=\"control-label\">SEO标题</label>";
						str+="<div class=\"controls\">";
						str+="<input class=\"span8\" type=\"text\" data_name=\""+result[i].name+"\" data_sort=\""+result[i].sort+"\" value=\""+result[i].title+"\" placeholder=\"请输入SEO标题\">";
						str+="<span class=\"help-inline\">建议不超过80个字符</span>";
						str+="</div>";
						str+="</div>";
						str+="<div class=\"control-group\">";
						str+="<label class=\"control-label\">SEO关键词</label>";
						str+="<div class=\"controls\">";
						str+="<div class=\"tags\" tabindex=\"1\" style=\"width: 66%;\" data_name=\""+result[i].name+"\" data_sort=\""+result[i].sort+"\"  id=\"seo_keywords"+i+"\">";
						str+=updateAddTag(result[i].keyword);   //SEO关键字加载
						str+="<input class=\"form-field-tags span9\" type=\"text\" placeholder=\"请输入SEO关键词\" style=\"display: none;\">";
						str+="<input type=\"text\" placeholder=\"请输入SEO关键词\"  class=\"tags_enter span9\" id=\"tags_enter"+i+"\" autocomplete=\"off\" />";
						str+="</div>";
						str+="<span class=\"help-inline\" style=\"margin-top: 10px;\">建议填写1-3个关键词，每填写1个关键词请敲键盘上的“回车键”!</span>";
						str+="</div>";
						str+="</div>";
						str+="<div class=\"control-group\">";
						str+="<label class=\"control-label\">SEO描述</label>";
						str+="<div class=\"controls\">";
						str+="<textarea class=\"span10\" data_name=\""+result[i].name+"\" data_sort=\""+result[i].sort+"\">"+result[i].describe+"</textarea>";
						str+="<span class=\"help-inline\">建议不超过200个字符</span>";
						str+="</div>";
						str+="</div>";
						str+="</div>";
						str+="</div>";
						strs = null;
					}
					$("#seo_list").html(str); 
				    $(".tags_enter").blur(function() { //焦点失去触发 
				        var txtvalue = $(this).val().trim();
				        if(txtvalue != '') {
				          addTag($(this));
				          $(this).parents(".tags").css({
				            "border-color": "#d5d5d5"
				          })
				        }
				      }).keydown(function(event) {
				        var key_code = event.keyCode;
				        var txtvalue = $(this).val().trim();
				        if(key_code == 13 && txtvalue != '') { //enter
				          addTag($(this));
				        }
				        if(key_code == 32 && txtvalue != '') { //space
				          addTag($(this));
				        }
				      });
				      $(document).on('click', '.close', function() {
				        $(this).parent(".tag").remove();
				      })

				      $(".tags").click(function() {
				        $(this).css({
				          "border-color": "#f59942"
				        })
				      }).blur(function() {
				        $(this).css({
				          "border-color": "#d5d5d5"
				        })
				      })

				      function addTag(objTag) {
				        var tag = objTag.val();
				        var id = objTag.attr("id");
				        if(tag != '') {
				          var i = 0;
				          $(".tag").each(function() {
				            if($(this).text() == tag + "×") {
				              $(this).addClass("tag-warning");
				              setTimeout("removeWarning()", 400);
				              i++;
				            }
				          })
				          objTag.val('');
//				          if(i > 0) { //说明有重复
//				            return false;
//				          }
				          $("#"+id+"").before("<span class='tag'>" + tag + "<button class='close' type='button'>×</button></span>"); //添加标签
				        }
				      }

				      function removeWarning() {
				        $(".tag-warning").removeClass("tag-warning");
				      }
				      
				      //修改seo标题
					    $('.span8').on("change", function () {
					        var id = $(this).parent().parent().parent().parent().attr('data_id');
					        var val = $(this).val();
					        var name = $(this).attr("data_name");
					        var sort = $(this).attr("data_sort");
					        var update = returnUpdate();	//获取当前时间
					  	  	var data = {
						  		id:		id,
						  		name:	name,
						  		sort:	sort,
						  		title:	val,
						  		update:		update,
					    	}
					  	 $.ajax({
					  		url:baseUrl+'/seoController/updateSeo',
					  		data : data,
					  		type : 'POST',
					  		dataType: "JSON",
					  		success : function(result) {
					  			if (result.state=="200") {
					  				
					  			}
					  		},
					  		error : function(error) {
					  			alert("获取产品内容列表失败！");
					  		}
					  	 });
					  });
				      //修改关键字----新增dom元素
					   $('.tags').on('DOMSubtreeModified',function(){
					        var id = $(this).parent().parent().parent().parent().attr('data_id');
					        var keywordId = $(this).attr("id");
					        var vals = "";
					        $.each($("#"+keywordId+">span"), function(i,val){
					        	vals+= val.textContent.replace('×', ',');
							  }); 
					        vals=vals.substring(0,vals.length-1);		//去掉最后一个逗号
					        var name = $(this).attr("data_name");
					        var sort = $(this).attr("data_sort");
					        var update = returnUpdate();	//获取当前时间
					  	  	var data = {
						  		id:		id,
						  		name:	name,
						  		sort:	sort,
						  		keyword:	vals,
						  		update:		update
					    	}
					  	 $.ajax({
					  		url:baseUrl+'/seoController/updateSeo',
					  		data : data,
					  		type : 'POST',
					  		dataType: "JSON",
					  		success : function(result) {
					  			if (result.state=="200") {
					  				
					  			}
					  		},
					  		error : function(error) {
					  			alert("获取产品内容列表失败！");
					  		}
					  	 });
					   })		
					    
				      //修改SEO描叙
					    $('.span10').on("change", function () {
					        var id = $(this).parent().parent().parent().parent().attr('data_id');
					        var val = $(this).val();
					        var name = $(this).attr("data_name");
					        var sort = $(this).attr("data_sort");
					        var update = returnUpdate();	//获取当前时间
					  	  	var data = {
						  		id:		id,
						  		name:	name,
						  		sort:	sort,
						  		describe:	val,
						  		update:		update
					    	}
					  	 $.ajax({
					  		url:baseUrl+'/seoController/updateSeo',
					  		data : data,
					  		type : 'POST',
					  		dataType: "JSON",
					  		success : function(result) {
					  			if (result.state=="200") {
					  				
					  			}
					  		},
					  		error : function(error) {
					  			alert("获取产品内容列表失败！");
					  		}
					  	 });
					  });
					   
					   var nexId = "disp1";
					   //点击显示或者折叠菜单
					  $('.layui-colla-title_1').on("click", function (e) {
						     if($(this).next().hasClass('layui-show')==true){	//判断点击的是否是当前打开的div,如果是，就关闭，如果不是，就打开当前的，并且关闭上一次的 
						    	 $(this).next().removeClass('layui-show');
						     }else  if($(this).next().attr('id') ==nexId){
						    	 $(this).next().addClass('layui-show');
						     } else{
						    	 $(this).next().addClass('layui-show');
						    	 $("#"+nexId+"").removeClass('layui-show');
						     }
						     nexId =  $(this).next().attr('id');
						     window.event.stopPropagation();//停止另外的点击事件
					  });
				}else{
					$("#seo_list").html("暂无数据");
				}
			},
			error: function (result) {
			}
		});
	}
	
	
	
	//SEO日志列表
	function readSeoListPage(pageNum){
		var read01 = $("#seo_type_select").val();		//查询字段
		var read02 = $("#seo_ziduan_val").val();		//查询内容
		var data = {
			currentPage : pageNum,
			pageSize : 8,
			ready01 : read01,
			ready02: read02
		};
		$.ajax({
			url:baseUrl+'/seoController/selectPagerListQueryString',
			data : data,
			type : 'POST',
			dataType: "JSON",
			success : function(data) {
				var dataLists = null;
				if (data != null) {
					dataLists = data.dataList;
					var str="";
					if (dataLists != null) {
						//当前第几页
						currentPage=data.currentPage;
						//一共多少页数据
						totalPage=data.totalPage;
						for(var i=0;i<dataLists.length;i++){
							str+="<tr>";
							str+="<td><input type=\"checkbox\" name=\"layTableCheckbox\" lay-skin=\"primary\" data_id=\""+ dataLists[i].id+"\"></td>";
							str+="<td>"+isNull(dataLists[i].update)+"</td>";
							str+="<td>"+isNull(dataLists[i].title)+"</td>";
							str+="<td>"+isNull(dataLists[i].keyword)+"</td>";
							str+="<td>"+isNull(dataLists[i].describe)+"</td>";
							str+="<td>"+isNull(dataLists[i].name)+"</td>";
							str+="<td>";
							str+="<div class=\"btn-group\">";
//							str+="<a href=\"javascript:;\" class=\"btn btn-mini btn-success check_leaveMessage\" title=\"详情\">详情</a>";
							str+="<a href=\"javascript:;\" class=\"btn btn-mini btn-delete delete_content\" title=\"删除\" onclick=\"deleteById('" + dataLists[i].id + "')\">删除</a>";
							str+="</div>";
							str+="</td>";
							str+="</tr>";
						}
						//构建分页信息
						loadPage(data);
					}else{
						$("#page").text("");
						str+="<div class=\"context_item\">";
						str+="<div class=\"notdata\">暂无数据</div>";
						str+="</div>";
					}
					$("#seolog_page_list").html(str);
				}
			},
			error : function(error) {
				alert("获取产品内容列表失败！");
			}
		});
	}
	
	//构建分页信息
	function loadPage(data){
		var pageStr = "";
		var pageNum = 0;
		var startindex = currentPage;  //当前页数
		var endindex = totalPage;  //总页数
		var spacenumb = endindex - startindex;
    	pageStr+="<div class=\"layui-box layui-laypage layui-laypage-default\" id=\"layui-laypage-1\">";
    	if(currentPage==1){
    		pageStr+="<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\"  id=\"prePage\">上一页</a>";
		}else{
			pageStr+="<a href=\"javascript:;\" class=\"layui-laypage-prev\"  id=\"prePage\">上一页</a>";
		}
    	if(currentPage==totalPage){
    		pageStr+="<a href=\"javascript:;\" class=\"layui-laypage-next layui-disabled\"   id=\"nextPage\">下一页</a>";
		}else{
			pageStr+="<a href=\"javascript:;\" class=\"layui-laypage-next\"   id=\"nextPage\">下一页</a>";
		}
    	pageStr+="<span class=\"layui-laypage-skip\">当前第 "+startindex+" 页";
    	pageStr+="</span>";
    	pageStr+="<span class=\"layui-laypage-limits\">合计"+data.totalPage+" 页 </span>";
    	pageStr+="<span class=\"layui-laypage-count\">共 "+data.totalRecord+" 条</span>";
    	pageStr+="</div>";
		$("#page").html(pageStr);
	}

	//上一页
	$(document).on("click","#prePage",function(){
		if(currentPage==1){
			alert("已经是第一页数据!");
			return false;
		}else{
			readSeoListPage(currentPage-1);
		}
	});

	//下一页
	$(document).on("click","#nextPage",function(){
		if(currentPage==totalPage){
			alert("已经是最后一页数据!");
			return false;
		}else{
			readSeoListPage(currentPage+1);
		}
	});

	//当前页数加载
	function pageNum(pageNum){
		readSeoListPage(pageNum);
	}

	//判断为数据库null
	function isNull(reason){
		if(reason!=null&&reason!=""&&reason!="null"){
			return reason;
		}else{
			return "";
		}
	}
	
	
	//搜索按钮
	$(document).on("click",".btn-inverse",function(e){
		readSeoListPage(currentPage);
	});
	
	//全部按钮
	$(document).on("click",".btn-success",function(e){
		$("#seo_ziduan_val").val('');  //设置查询条件为全部
		$("#seo_type_select").val('0');  //清空查询内容
		readSeoListPage("1");
	});
	
	
	
	//删除单个
	function deleteById(id){
		if(confirm('确定删除该条内容吗？')) {
			var array=new Array();
			array[0] = parseInt(id);
			deleteRecord(array);
	      } else {
	        console.log('取消')
	        return;
	      }
	}
	
	//批量选中/取消
	$(document).on("click","#checkedAll",function(e){
	    var chilles = $("input:checkbox[name=layTableCheckbox]");
	    if ($(this).is(':checked')) {
		    for (var i = 0; i < chilles.length; i++) {
		    	chilles[i].checked  = true;
		    }
	    } else {
		    for (var i = 0; i < chilles.length; i++) {
		    	chilles[i].checked  = false;
		    }
	    }
	});
	
	//批量删除
	$(document).on("click","#deleteAll",function(e){
		var ifched = "1";   
		var chilles = $("input:checkbox[name=layTableCheckbox]");
		for (var i = 0; i < chilles.length; i++) {
			if(chilles[i].checked == true){
				ifched = "2";
			}
	    }
		if(ifched=="1"){   //判断是否有勾选项目
			alert("请勾选需要删除的内容。");
			return;
		}else{
			if(confirm('确定删除选中的内容吗？')) {
				var array=new Array();
				var index = 0;
				for (var i = 0; i < chilles.length; i++) {
					if(chilles[i].checked  == true){
						array[index] = chilles[i].getAttribute('data_id');
						index++;
					}
			    }
				deleteRecord(array);
				$("#checkedAll").checked = false;
		      } else {
		        console.log('取消')
		        return;
		      }
		}
	});
	
	//删除信息
	function deleteRecord(array){
		var data = {ids:array};
			$.ajax({
				url:baseUrl+'/seoController/deleteSeoLog',
				data : data,
				type : 'POST',
				dataType: "JSON",
				traditional: true, //默认fals
				success : function(result) {
					if(result.state=="200"){
						alert("删除成功。");
						readSeoListPage("1");
					}else{
						alert("删除失败。");
					}
			},
			error: function (result) {
			}
		});
	}
	
	
		
		//返回当前时间方法  格式 xxxx-xx-xx xx:xx:xx
		function returnUpdate(){
			var date=new Date();   
			var year=date.getFullYear(); //获取当前年份   
			var mon=date.getMonth()+1; //获取当前月份   
			var da=date.getDate(); //获取当前日   
			var h=date.getHours(); //获取小时   
			var m=date.getMinutes(); //获取分钟   
			var s=date.getSeconds(); //获取秒   
			if(mon.toString().length==1){
				mon="0"+mon;
			}
			if(da.toString().length==1){
				da="0"+da;
			}
			if(h.toString().length==1){
				h="0"+h;
			}
			if(m.toString().length==1){
				m="0"+m;
			}
			if(s.toString().length==1){
				s="0"+s;
			}
			return year+'-'+mon+'-'+da+' '+h+':'+m+':'+s; 
		}
		
		
		//关键字加载
	    function updateAddTag(tags) {
	    	var str ="";
	    	var strs = new Array();
	    	strs = tags.split(",");
	    	if(strs.length>=1){
	    		for(var i=0;i<strs.length;i++){
	    			str+="<span class='tag'>" + strs[i] + "<button class='close span11' type='button'>×</button></span>"; //添加标签
		    	}
	    	}else{
	    		str+="<span class='tag'>" + tags + "<button class='close span11' type='button'>×</button></span>"; //添加标签
	    	}
	    	return str;
	      }
		
		
		

